Create Project: springboot_service_interface_autowired (add Spring Boot Starters from the table)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside package controllers)
Create Package: services (inside main package)
– Create Class: MyServiceInterface.java (inside package controllers)
– Create Class: MyServiceImplementation.java (inside package controllers)
MyServiceInterface.java
package com.ivoronline.springboot_service_interface_autowired.services;
public interface MyServiceInterface {
public String hello();
}
MyServiceImplementation.java
package com.ivoronline.springboot_service_interface_autowired.services;
import org.springframework.stereotype.Service;
@Service
public class MyServiceImplementation implements MyServiceInterface {
public String hello() {
return "Hello from Service";
}
}
MyController.java
package com.ivoronline.springboot_service_interface_autowired.controllers;
import com.ivoronline.springboot_service_interface_autowired.services.MyServiceInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@Autowired MyServiceInterface myService;
@ResponseBody
@RequestMapping("/Hello")
public String hello() {
//CALL SERVICE
String result = myService.hello();
//RETURN RESULT
return result;
}
}